import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fenzi/pages/edit.dart';
import 'package:fenzi/global.dart';
import 'package:fenzi/manager/main_manager.dart';
import 'package:provider/provider.dart';
import 'package:fenzi/pages/list_item.dart';

class ListPage extends StatefulWidget {
  ListPage({Key key, this.type}) : super(key: key);
  final String type;
  @override
  State<StatefulWidget> createState() => _ListPageState();
}

class _ListPageState extends State<ListPage> {
  String getTypeName(type) {
    String zhName;
    switch (type) {
      case 'total':
        zhName = '全部';
        break;
      case 'male':
        zhName = '男方';
        break;
      case 'famale':
        zhName = '女方';
        break;
      case 'child':
        zhName = '孩子';
        break;
      case 'friend':
        zhName = '朋友';
        break;
      case 'other':
        zhName = '其他';
        break;
      default:
        zhName = '其他';
    }
    return zhName;
  }

  List dataFilter(list, type) {
    if (type == 'total') return list;
    return list
        .where((item) => jsonDecode(item['data'])['type'] == type)
        .toList();
  }

  List<Widget> getList(allLocalCache) {
    List<Widget> list = [];
    for (var i = 0; i < allLocalCache.length; i++) {
      var item = json.decode(allLocalCache[i]['data']);
      list.add(ListItem(data: item, id: allLocalCache[i]['id']));
    }
    if (list.length == 0) {
      list = [
        SizedBox(height: 40),
        Container(
          width: 200,
          child: Image.asset('assets/images/none.png'),
        ),
        SizedBox(height: 40),
        Center(
          child: Text(
            "没有该类记录，快去记一笔吧！",
            style: TextStyle(
              color: Global.fontSecondColor,
            ),
          ),
        ),
      ];
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    MainManager _mainManager = Provider.of<MainManager>(context);
    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(color: Global.backgroundColor),
        toolbarHeight: 50,
        elevation: 0,
        title: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              "随份子 - ${getTypeName(widget.type)}账簿",
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                color: Global.backgroundColor,
                fontSize: 15,
                fontWeight: FontWeight.w800,
              ),
            ),
            InkWell(
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => EditPage(id: '000', data: {
                      "spending": false,
                      "date": DateTime.now().toString().substring(0, 10),
                      "name": "",
                      "relation": "",
                      "co": "",
                      'type': widget.type == 'total' ? 'other' : widget.type,
                      "remark": ""
                    }),
                  ),
                );
              },
              child: Container(
                padding: EdgeInsets.fromLTRB(10, 6, 10, 6),
                decoration: BoxDecoration(
                  color: Global.backgroundColor,
                  border: Border.all(color: Global.primaryColor, width: 1),
                  borderRadius: BorderRadius.circular(40),
                ),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(
                      Icons.add_circle_outlined,
                      color: Global.primaryColor,
                      size: 18,
                    ),
                    SizedBox(width: 5),
                    Text(
                      '记一笔',
                      style: TextStyle(
                        color: Global.primaryColor,
                        fontSize: 14,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(color: Global.cardBackgroundColor),
        child: SingleChildScrollView(
          child: Column(
            children:
                getList(dataFilter(_mainManager.allLocalCache, widget.type)),
          ),
        ),
      ),
    );
  }
}
